/*
 * Author: yezi
 * E-mail: okye328@gmail.com
 * Create time: Dec 26, 2012 9:36:28 PM
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 *
 */

package com.kanbox.linux.operation;

import org.apache.http.client.methods.HttpUriRequest;

import com.alibaba.fastjson.JSONObject;
import com.kanbox.linux.operation.http.HttpExecutor;
import com.kanbox.linux.operation.http.RequestFactory;
import com.kanbox.linux.operation.http.RequestFactory.RequestType;
import com.kanbox.linux.token.TokenManager;

public class Information {
	private String email;
	private boolean emailIsActive;
	private String phone;
	private boolean phoneIsActive;
	private long spaceQuota;// Total space
	private long spaceUsed;

	public Information() {
		HttpUriRequest httpRequest = RequestFactory.buildHTTPRequest(
				RequestFactory.INFO, "", RequestType.GET,
				TokenManager.getAccessToken());
		JSONObject jo = HttpExecutor.executeForJSON(httpRequest);
		email = jo.getString("email");
		emailIsActive = jo.getBooleanValue("emailIsActive");
		phone = jo.getString("phone");
		phoneIsActive = jo.getBooleanValue("phoneIsActive");
		spaceQuota = jo.getLongValue("spaceQuota");
		spaceUsed = jo.getLongValue("spaceUsed");
	}

	public String getEmail() {
		return email;
	}

	public boolean isEmailIsActive() {
		return emailIsActive;
	}

	public String getPhone() {
		return phone;
	}

	public boolean isPhoneIsActive() {
		return phoneIsActive;
	}

	/**
	 * 
	 * @param UNIT
	 *            Like B, KB, MB, GB
	 * @return
	 */
	public String getSpaceQuota(String UNIT) {
		if (UNIT == null || UNIT.equalsIgnoreCase("b")) {
			return spaceQuota + "B";
		} else if (UNIT.equals("KB")) {
			return (spaceQuota / 1024) + "KB";
		} else if (UNIT.equals("MB")) {
			return (spaceQuota / 1024 / 1024) + "MB";
		} else if (UNIT.equals("GB")) {
			return (spaceQuota / 1024 / 1024 / 1024) + "GB";
		} else {
			return spaceQuota + "B";
		}
	}

	/**
	 * 
	 * @param UNIT
	 *            Like B, KB, MB, GB
	 * @return
	 */
	public String getSpaceUsed(String UNIT) {
		if (UNIT == null || UNIT.equalsIgnoreCase("b")) {
			return spaceUsed + "B";
		} else if (UNIT.equals("KB")) {
			return (spaceUsed / 1024) + "KB";
		} else if (UNIT.equals("MB")) {
			return (spaceUsed / 1024 / 1024) + "MB";
		} else if (UNIT.equals("GB")) {
			return (spaceUsed / 1024 / 1024 / 1024) + "GB";
		} else {
			return spaceUsed + "B";
		}
	}

	@Override
	public String toString() {
		return "[email=" + email + ", emailIsActive=" + emailIsActive
				+ ", phone=" + phone + ", phoneIsActive=" + phoneIsActive
				+ ", spaceQuota=" + spaceQuota + ", spaceUsed=" + spaceUsed
				+ "]";
	}

	public String getSpaceUsedRate() {
		double rate = (double) spaceUsed / (double) spaceQuota;
		rate = rate * 10000;
		rate = Math.round(rate);
		rate = rate / 100.0;
		return rate + "%";
	}

	public long getTotalSpace() {
		return spaceQuota;
	}

	public long getUsedSpace() {
		return spaceUsed;
	}
}
